﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using PPAP.Model;
using System.Data;

namespace PPAP.Web.Part
{
    public partial class PPARDateEdit : PageBase
    {
        PPARBO pbo = new PPARBO();
        LogBO lbo = new LogBO();
        MailTemplateBO mbo = new MailTemplateBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    PPAR pp = pbo.GetModel(int.Parse(Request.QueryString["ID"].ToString()));
                    if (pp != null)
                    {
                        lblPartNo.Text = "PPAR-" + pp.PPARNO + "." + pp.PPARVERSION;
                        lblPPARdate.Text = pp.Planedppardate.ToString("yyyy-MM-dd");
                        txtNewDate.Text = pp.Planedppardate.ToString("yyyy-MM-dd");
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["ID"] != null)
            {
                txtComment.Text = CheckWord(txtComment.Text);
                PPAR pp = pbo.GetModel(int.Parse(Request.QueryString["ID"].ToString()));
                if (pp != null)
                {
                    if (txtComment.Text.Trim() == "")
                    {
                        AlertMessage("Please enter a comment");
                        return;
                    }
                    pp.Planedppardate = DateTime.Parse(txtNewDate.Text);
                   int num=  pbo.UpdateForModifyDate(pp);
                   lbo.WriteLog(CurrentUser.Id, string.Format("Change PPAP date:{0} to {1}  (comment:{2} ) ", lblPPARdate.Text, txtNewDate.Text, txtComment.Text), Request.QueryString["ID"].ToString());
                   if (num > 0)
                   {

                       //发邮件

                       MailTemplate mail = null;
                           mail = mbo.GetModel(18);
                           DataSet dsmail = mbo.GetEDM3(Request.QueryString["ID"].ToString());
                       if (mail != null && dsmail.Tables[0].Rows.Count > 0)
                       {
                           foreach (DataRow dr in dsmail.Tables[0].Rows)
                           {
                               string title = mail.TITLE.Replace("{Part No.}", dr["bdntpartno"].ToString());
                               title = title.Replace("{Part name}", dr["bdntpartname"].ToString());
                               title = title.Replace("{Color}", dsmail.Tables[0].Rows[0]["color"].ToString());
                               mail.CONTENT = mail.CONTENT.Replace("{Date1}", lblPPARdate.Text);
                               mail.CONTENT = mail.CONTENT.Replace("{Date2}", txtNewDate.Text);
                               try
                               {
                                   base.SendEmailToUser(dr["username"].ToString(), title, mail.CONTENT, null);
                                   lbo.WriteLog(CurrentUser.Id, "Send Email:" + dr["username"].ToString(), Request.QueryString["id"].ToString());

                               }
                               catch (Exception ex)
                               {
                                   AlertMessage(ex.Message.ToString());
                                   lbo.WriteLog(CurrentUser.Id, "Send Email:" + ex.Message.ToString(), Request.QueryString["id"].ToString());
                               }
                           }
                       }

                       //}
                       //发邮件

                       base.AlertReflashOpener("Save success");
                   }
                }
            }
        }
    }
}